/**
 * @名称: msg,alert,confirm,loading,toast model of layer
 * @更新: 2021.01.27
 * @作者: yuguoliang
 * @博客：http://www.cssw3c.com
 */
;(function (window, document, undefined) {
    var isJQuery = window.jQuery;
    var $;
    if (isJQuery) {
        $ = window.jQuery;
    } else if (window.$) {
        $ = window.$;
    } else {
        $ = window.$ = new Object();
    }

    var layer = window.layer;

    $.msg = function (msg, direct, time, type) {
        var config;
        if (typeof msg === 'object') {
            config = msg;
        } else {
            config = {
                msg: msg,
                direct: direct,
                time: time,
                type: type
            }
        }
        config.msg = config.msg ? config.msg : '操作成功';
        config.direct = config.direct ? config.direct : 'middle';
        config.time = config.time ? config.time : 1.5;
        config.type = (config.type === undefined) ? 1 : config.type;
        config.anim = config.anim ? config.anim : 'up';
        var html, className, style='';
        if (config.direct == 'middle') {
            className = 'weui-toast-msg';
            style = '';
            html = '<span class="weui-tmsg">' + config.msg + '</span>';
        } else if (config.direct == 'bottom') {
            className = 'weui-toast-msg weui-bottom';
            html = '<span class="weui-tmsg">' + config.msg + '</span>';
        }
        layer.open({
            type: config.type,
            className: className,
            shadeClose: false,
            style: style + ' ' + config.style,
            shade: false,
            time: config.time,
            anim: config.anim,
            content: html,
        });
    };

    $.alert = function (msg, title, onOK) {
        var config;
        if (typeof msg === 'object') {
            config = msg;
        } else {
            if (typeof title === 'function') {
                callback = arguments[1];
                title = undefined;
            }
            config = {
                msg: msg,
                title: title,
                onOK: onOK
            }
        }
        var skin = config.skin ? config.skin : 'weui-dialog';
        var html = '<div class="' + (skin + '_bd') + '">' + config.msg + '</div>';
        layer.open({
            className: config.title ? '' : 'title-no',
            title: config.title,
            style: config.style ? config.style : 'width: 80%;max-width: 300px;',
            skin: skin,
            shadeClose: false,
            content: html,
            btn: ['确定'],
            success: function () {
                if (isJQuery) {
                    $(".layui-m-layershade").on("touchmove", function (e) {
                        e.preventDefault();
                    });
                    $('.layui-m-layermain').on('touchmove', function (e) {
                        e.preventDefault();
                    });
                }
            },
            yes: config.onOK
        });
    };

    $.confirm = function (msg, title, onOK, onCancel) {
        var config;
        if (typeof msg === 'object') {
            config = msg;
        } else {
            if (typeof title === 'function') {
                onOK = arguments[2];
                onCancel = arguments[1];
                title = undefined;
            }
            config = {
                title: title,
                msg: msg,
                onOK: onOK,
                onCancel: onCancel
            }
        }
        var skin = config.skin ? config.skin : 'weui-dialog';
        var html = '<div class="' + (skin + '_bd') + '">' + config.msg + '</div>';
        layer.open({
            className: config.title ? '' : 'title-no',
            title: config.title,
            style: config.style ? config.style : 'width: 80%;max-width: 300px;',
            skin: skin,
            shadeClose: false,
            content: html,
            btn: config.btn ? config.btn : ['确定', '取消'],
            success: function () {
                if (isJQuery) {
                    $(".layui-m-layershade").on("touchmove", function (e) {
                        e.preventDefault();
                    });
                    $('.layui-m-layermain').on('touchmove', function (e) {
                        e.preventDefault();
                    });
                }
            },
            yes: config.onOK,
            no: config.onCancel
        });
    };

    $.loading = function (msg, type) {
        var layerIndex;
        var config;
        if (typeof msg === 'object') {
            config = msg;
        } else {
            config = {
                msg: msg,
                type: type
            }
        }
        config.msg = config.msg ? config.msg : '数据加载中';
        config.type = (config.type === undefined) ? 0 : config.type;
        var html = '';
        html += '<i class="weui-loading weui-icon_toast"></i>';
        html += '<p class="weui-toast__content">' + config.msg + '</p>';
        layerIndex = layer.open({
            type: config.type,
            className: 'weui-toast',
            shadeClose: false,
            shade: 'background-color: rgba(0,0,0,0)',
            content: html,
            success: function () {
                if (isJQuery) {
                    $(".layui-m-layershade").on("touchmove", function (e) {
                        e.preventDefault();
                    });
                    $('.layui-m-layermain').on('touchmove', function (e) {
                        e.preventDefault();
                    });
                }
            }
        });
        return layerIndex;
    };

    //toast 用于临时显示某些信息，并且会在数秒后自动消失。这些信息通常是轻量级操作的成功、失败或等待状态信息。
    $.toast = function (msg, style, time, callback) {
        var config;
        if (typeof msg === 'object') {
            config = msg;
        } else if (typeof style === "function") {
            callback = style;
            style = 'success';
            config = {
                msg: msg,
                style: style,
                time: time,
                callback: callback
            }
        } else {
            config = {
                msg: msg,
                style: style,
                time: time,
                callback: callback
            }
        }
        config.msg = config.msg ? config.msg : '操作成功';
        config.style = (config.style === undefined) ? 'success' : config.style;
        config.time = config.time ? config.time : 2;
        var className;
        switch (config.style) {
            case 'success':
                className = 'weui-icon-success-no-circle';
                break;
            case 'error':
                className = 'weui-icon-cancel';
                break;
            default:
                className = 'weui-icon-success-no-circle';
                break;
        }
        var html = '';
        html += '<i class="' + className + ' weui-icon_toast"></i>';
        html += '<p class="weui-toast__content">' + config.msg + '</p>';
        layer.open({
            className: 'weui-toast',
            shadeClose: false,
            shade: 'background-color: rgba(0,0,0,0)',
            time: config.time,
            content: html,
            success: function () {
                if (isJQuery) {
                    $(".layui-m-layershade").on("touchmove", function (e) {
                        e.preventDefault();
                    });
                    $('.layui-m-layermain').on('touchmove', function (e) {
                        e.preventDefault();
                    });
                }
            },
            end: function () {
                if (config.callback) {
                    config.callback.call(this);
                }
            }
        });
    };
})(window, document);
